import { http } from '@/utils/http'
import { useUserStore } from '@/stores/'

/**
 * 登录
 * @param {Object} params 请求参数
 * @param {String} params.username 用户名
 * @param {String} params.password 密码
 * @returns
 */
export const postLoginAPI = (params) => {
  return http({
    method: 'POST',
    url: '/web/session/authenticate',
    data: {
      db: 'odoo16-test',
      login: params.username,
      password: params.password,
    },
  })
}

/**
 * 获取工单
 * @returns
 */
export const postOrderListAPI = async () => {
  const userStore = useUserStore()
  // const orderStore = useOrderStore()
  // if (await isNetWork()) {
  //   const res = await http({
  //     method: 'POST',
  //     url: '/web/dataset/call_kw',
  //     data: {
  //       model: 'dtw.workmanship',
  //       method: 'web_search_read',
  //       args: [],
  //       kwargs: {
  //         limit: 0,
  //         offset: 0,
  //         order: '',
  //         count_limit: 9999,
  //         // domain: [['dispatch', '=', 'True']],
  //         domain: [
  //           ['dispatch', '=', 'True'],
  //           ['company_id', '=', userStore.userInfo.company_id],
  //         ],
  //         fields: [],
  //         context: {
  //           allowed_company_ids: [userStore.userInfo.company_id],
  //           ...userStore.userInfo.user_context,
  //         },
  //       },
  //     },
  //   })
  //   orderStore.setOrderList(res.records)
  // }
  // return orderStore.orderList
  return http({
    method: 'POST',
    url: '/web/dataset/call_kw',
    data: {
      model: 'dtw.workmanship',
      method: 'web_search_read',
      args: [],
      kwargs: {
        limit: 0,
        offset: 0,
        order: '',
        count_limit: 9999,
        // domain: [['dispatch', '=', 'True']],
        domain: [
          ['dispatch', '=', 'True'],
          ['company_id', '=', userStore.userInfo.company_id],
        ],
        fields: [],
        context: {
          allowed_company_ids: [userStore.userInfo.company_id],
          ...userStore.userInfo.user_context,
        },
      },
    },
  })
}

/**
 * 获取设备信息
 * @param {string} deviceId 设备ID
 * @returns
 */
export const postDeviceInfoAPI = (deviceId) => {
  const userStore = useUserStore()
  let domain = null
  if (deviceId) {
    domain = [
      ['company_id', '=', userStore.userInfo?.company_id],
      ['deviceID', '=', deviceId],
    ]
  } else {
    domain = [['company_id', '=', userStore.userInfo?.company_id]]
  }
  return http({
    method: 'POST',
    url: '/web/dataset/call_kw',
    data: {
      model: 'dtw.wrench.info',
      method: 'web_search_read',
      args: [],
      kwargs: {
        context: {
          // allowed_company_ids: [wx.$userinfo.company_id],
          allowed_company_ids: [userStore.userInfo.company_id],
          // ...wx.$userinfo.user_context,
          ...userStore.userInfo.user_context,
        },
        //   domain: [ ["company_id", "=", wx.$userinfo.company_id] ],
        domain: domain,
        fields: ['name', 'proto', 'company_id', 'deviceID'],
      },
    },
  })
}

/**
 * 数据上传服务器
 * @param {Array} params 工作数据
 */
export const postSaveWorkDataAPI = (params) => {
  const userStore = useUserStore()
  return http({
    method: 'POST',
    url: '/web/dataset/call_kw',
    data: {
      model: 'dtw.workdata',
      method: 'create',
      args: [params],
      kwargs: {
        context: {
          allowed_company_ids: [userStore.userInfo.company_id],
          ...userStore.userInfo.user_context,
        },
      },
    },
  })
}
